import { Asset, SpriteFrame } from "cc";
import { BundleMgr } from "../../mgr/BundleMgr";
import { ILoading } from "./ILoading";


export class LoadingPublic implements ILoading {
    loadedProgress: number = 0;
    readonly totalProgress: number = 1;

    private _arr: { res: string, tType: any }[] = [
        // {
        //     res: "btn_blue", tType: SpriteFrame
        // }
    ]
    bLoading: boolean;
    bCompleted: boolean;
    constructor(
        public bundleMgr: BundleMgr
    ) {

    }

    load(): void {
        for (const { res, tType } of this._arr) {
            this.bundleMgr.public.load(res, tType, (err: any, data: Asset) => {
                this.loadedProgress++;
            })
        }

        if (this.bLoading) {
            return;
        }
        this.bLoading = true;

        this.loadedProgress++;
    }
    checkCompleted(): boolean {
        if (this.bCompleted) {
            return true;
        }
        const ret = this.loadedProgress >= this.totalProgress;        
        if (ret) {
            this.bCompleted = ret;
            this.target.onLoadingCompleted();
            return true;
        }
        return false;
    }

    getProgress(): number {
        return this.loadedProgress / this.totalProgress;
    }

    getProgressDesc(): string {
        return "正在加载公共资源..." + this.getProgress();
    }
    target: { onLoadingCompleted(): void };

}